define(['moment', 'd3-MindMap'], function (moment) {
    return ['$scope', '$filter', '$stateParams', 'config', 'MMWService', '$mdDialog', '$mdMindMap',
        function ($scope, $filter, $stateParams, config, $MMWService, $mdDialog, $mdMindMap) {

            $scope.init = function(){
                $scope.dashboard = {
                    plotno_details : {
                        templateUrl : 'module/moduleQT/program/dashboard-QT02/QT02_plotNo.html'
                    },
                    inspectmsg_machine_details : {
                        templateUrl : 'module/moduleQT/program/dashboard-QT02/QT02_inspectMsg_machine.html'
                    },
                    productmsg_machine_details : {
                        templateUrl : 'module/moduleQT/program/dashboard-QT02/QT02_productMsg_machine.html'
                    },
                    // 交易信息
                    trade_details: {
                        templateUrl: 'module/moduleQT/program/dashboard-QT02/QT02_tradeInfo.html'
                    },
                    // 收集信息
                    collect_details: {
                        templateUrl: 'module/moduleQT/program/dashboard-QT02/QT02_collectInfo.html'
                    },
                    employee_details: {
                        templateUrl : 'module/moduleQT/program/dashboard-QT02/QT02_employee.html'
                    },
                    material_details: {
                        templateUrl : 'module/moduleQT/program/dashboard-QT02/QT02_material.html'
                    },
                    // SPC信息
                    spc_details: {
                        templateUrl: 'module/moduleQT/program/dashboard-QT02/QT02_spcInfo.html'
                    }
                };

                // // 預設 dashboard畫面
                $scope.dashboardPanel = ['plotno_details','inspectmsg_machine_details','productmsg_machine_details'];
            };

            // 篩選 Bom資料
            $scope.filterCondition = function(){
                $mdDialog.serviceDialog('QT02_filterConditionService',function(data, batch_no){
                    $scope.bom_data = data;
                    $scope.bom_data.sort((a, b) => a.lev - b.lev)
                    // 切換 Bom資料，清空當前的模版資料。
                    $scope.QT02_dashboard = {};
                    if($scope.bom_data && $scope.bom_data.length > 0){
                        $scope.getDashboardData(data[0]);
                    }
                });
            };

            // 點擊BOM資料取得模版資料
            $scope.getDashboardData = function(item){
                var content;
                // 只有第一層 bom資料帶入 praent_lot
                if(item.lev == '0'){
                    $scope.batch_no = item.praent_lot;
                    content = {
                        base_data:{},
                        subset_query_conditon:[
                            {
                                mode_type: $scope.dashboardPanel[0],
                                lot_code: item.praent_lot
                            },
                            {
                                mode_type: $scope.dashboardPanel[1],
                                lot_code: item.praent_lot
                            },
                            {
                                mode_type: $scope.dashboardPanel[2],
                                lot_code: item.praent_lot
                            },
                            {
                                mode_type: 'production_structure',
                                lot_code: item.praent_lot
                            }
                        ]
                    };
                } else {
                    $scope.batch_no = item.sub_lot;
                    content = {
                        base_data:{},
                        subset_query_conditon:[
                            {
                                mode_type: $scope.dashboardPanel[0],
                                lot_code: item.sub_lot
                            },
                            {
                                mode_type: $scope.dashboardPanel[1],
                                lot_code: item.sub_lot
                            },
                            {
                                mode_type: $scope.dashboardPanel[2],
                                lot_code: item.sub_lot
                            },
                            {
                                mode_type: 'production_structure',
                                lot_code: item.sub_lot
                            }
                        ]
                    };
                };
                dashboardDetails(content, function(data){
                    $scope.QT02_dashboard = data;
                    GenerateMindMap($scope.QT02_dashboard.production_structure);
                });
            };

            // 調整 bom資料版面
            $scope.bomData_style = function(item){
                return {marginLeft: item.lev * 25 + 'px'};
            };

            // 開關 dashboard
            $scope.isInnerContentHide = false;
            $scope.togglePanel = function(sectionName, $event){
                var target = $scope.dashboard[sectionName];
                if(sectionName == $scope.dashboardPanel[1] || sectionName == $scope.dashboardPanel[2]){
                    $scope.isInnerContentHide = !$scope.isInnerContentHide;
                } else {
                    target.isContentHide = !target.isContentHide;
                    var el = document.querySelectorAll('.section');
                    for(var i = 0; i < el.length; i++){
                        el[i].style.transition = "all ease-in-out .25s";
                    };
                };
            };

            // 切換 dashboard
            $scope.changePanel = function(event, panelCode){
                event.stopPropagation();
                var panelList = [
                    {QT02_dashboard_key:'QT02_employee_details', panelCode : 'employee_details', panelName: $filter('translate')('dashboard-QT02.employeeInfo.title')},
                    {QT02_dashboard_key:'QT02_material_details', panelCode : 'material_details', panelName: $filter('translate')('dashboard-QT02.materialInfo.title')},
                    {QT02_dashboard_key: 'QT02_productMsg_machine_details', panelCode: 'productmsg_machine_details', panelName: $filter('translate')('dashboard-QT02.productMsg_machineInfo.title') },
                    {QT02_dashboard_key: 'QT02_tradeInfo_details', panelCode: 'trade_details', panelName: $filter('translate')('dashboard-QT02.tradeInfo.title')},
                    {QT02_dashboard_key: 'QT02_collectInfo_details', panelCode: 'collect_details', panelName: $filter('translate')('dashboard-QT02.collectInfo.title')},
                    {QT02_dashboard_key:'QT02_inspectMsg_machine_details', panelCode : 'inspectmsg_machine_details', panelName: $filter('translate')('dashboard-QT02.inspectMsg_machineInfo.title')},
                    {QT02_dashboard_key: 'QT02_plotNo_details', panelCode: 'plotno_details', panelName: $filter('translate')('dashboard-QT02.plotNoInfo.title')},
                    {QT02_dashboard_key: 'QT02_spcInfo_details', panelCode: 'spc_details', panelName: $filter('translate')('dashboard-QT02.spcInfo.title')}
                ];
                $scope.$root.showSelect({
                    title : $filter('translate')('common.msg.plz_select'),
                    label : 'panelName',
                    code : 'panelCode',
                    selectCode : panelCode,
                    list : panelList,
                    confirm : function(item, dialog){
                        var now_panel_index = $scope.dashboardPanel.indexOf(panelCode);
                        var select_panel_index = $scope.dashboardPanel.indexOf(item.panelCode);
                        //當為-1時表示選擇的不再列表內，則不用做交換
                        if(select_panel_index != -1){
                            $scope.dashboardPanel[select_panel_index] = panelCode;
                        }
                        // 調整動畫時間
                        var el = document.querySelectorAll('.section');
                        for(var i = 0; i < el.length; i++){
                            el[i].style.transition = "all ease-in-out 0s";
                        };
                        $scope.dashboardPanel[now_panel_index] = item.panelCode;
                        localStorage.setItem(config.appName + '_QT02_panel', JSON.stringify($scope.dashboardPanel));

                        // 更換模版帶出模版資料
                        var content = {
                            base_data:{},
                            subset_query_conditon:[
                                {
                                    // P.S. mode_type 傳的字串全都要小寫。
                                    mode_type: item.panelCode,
                                    lot_code: $scope.batch_no
                                }
                            ]
                        };
                        // 如果沒有選擇 bom資料，就只會單純切換模板，不會開啟服務
                        if($scope.bom_data && $scope.bom_data.length > 0){
                            dashboardDetails(content, function(data){
                                if(typeof(data) == 'object'){
                                    // 帶回選擇的那筆模板資料
                                    $scope.current_dashboard_data = [];
                                    for(var i in data){
                                        if(data[i].length != 0){
                                            $scope.current_dashboard_data.unshift(data[i]);
                                        }
                                    };
                                    $scope.QT02_dashboard[item.QT02_dashboard_key] = $scope.current_dashboard_data[0];
                                } else {
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                                };
                            });
                        }
                        dialog.hide();
                    }
                });
            };

            // 儲存模版
            // $scope.model_no : 1(正向追溯)、2(生產追溯)
            $scope.model_no = '2';
            $scope.saveModule = function(){
                $mdDialog.serviceDialog('SaveModuleService',$scope.model_no,$scope.dashboardPanel);
            };

            // 載入模版
            $scope.loadModule = function(){
                $mdDialog.serviceDialog('LoadModuleService',$scope.model_no,function(data){
                    $scope.dashboardPanel = data;
                });
            };

            // 匯出功能
            $scope.export = function(){
                var condition = $scope.$root.genConditioninfo();
                exportExcel($stateParams.data.report_id, condition);
            };

            function exportExcel(report_id, condition, callback){
                if(window.nodeRequire){

                    try {
                        var dialog = window.nodeRequire('electron').remote.dialog;
                        dialog.showOpenDialog({
                            properties: ['openDirectory']
                        },function(fileList){
                            if(fileList && fileList.length>0){
                                $scope.$root.showToast($filter('translate')('common.msg.file_downloading'));
                                $MMWService.sendToServer({
                                    uri : 'ServicesSTD.Module_SYS.user_custom_report_excel_get',
                                    content : {
                                        report_id:report_id,
                                        query_condition_info:condition
                                    },
                                    success : function(data){
                                        if(callback){
                                            callback(data);
                                        } else {
                                            var file_data = Buffer.from(data.file_data, 'base64');
                                            var file_name = fileList[0]+'\\'+$scope.title+moment().format('YYYYMMDDHHmmss')+'.xls';
                                            window.nodeRequire('fs').writeFile(file_name, file_data, function(error){
                                                if(error){
                                                    $scope.$root.showAlert($filter('translate')('common.msg.cantDownloadFile')+error);
                                                } else {
                                                    $mdlToast.hide();
                                                    $scope.$root.showConfirm($filter('translate')('common.msg.file_download_complete_and_open'), function(dialog){
                                                        dialog.hide();
                                                        try {
                                                            window.nodeRequire('electron').shell.openItem(file_name);
                                                        } catch (error) {
                                                            nw.Shell.openItem(file_name);
                                                        }
                                                    });
                                                };
                                            });
                                        };
                                    }
                                }, false);
                            }
                        });
                    } catch (error) {
                        $rootScope.showToast($filter('translate')('common.msg.file_downloading'));
                        $MMWService.sendToServer({
                            uri : 'ServicesSTD.Module_SYS.user_custom_report_excel_get',
                            content : {
                                report_id:report_id,
                                query_condition_info:condition
                            },
                            success : function(data){
                                if(callback){
                                    callback(data);
                                } else {
                                    var fs = window.nodeRequire('fs');

                                    var filePath = window.nodeRequire('path').dirname(process.execPath)+'\\Custom_Report_Excel\\';
                                    var file_data = Buffer.from(data.file_data, 'base64');

                                    if(!fs.existsSync(filePath)){
                                        fs.mkdirSync(filePath);
                                    }

                                    var file_name = filePath+$scope.title+moment().format('YYYYMMDDHHmmss')+'.xls';
                                    fs.writeFile(file_name, file_data, function(error){
                                        if(error){
                                            $rootScope.showAlert($filter('translate')('common.msg.cantDownloadFile')+error);
                                        } else {
                                            $mdlToast.hide();
                                            $rootScope.showConfirm($filter('translate')('common.msg.file_download_complete_and_open'), function(dialog){
                                                dialog.hide();
                                                nw.Shell.openItem(file_name);
                                            });
                                        }
                                    });
                                }
                            }
                        }, false); 
                    }

 
                } else {
                    $scope.$root.showToast($filter('translate')('common.msg.file_download_complete'));
                };
            };

            // 取得模版資料
            function dashboardDetails(content, callback){
                // var data = {
                //     QT02_productNo_details:[{
                //         material_className: '通訊群組',
                //         graph_No: '無',
                //         description: 'P22017062901',
                //         stock_unit_name: '個',
                //         effective_date: '2019/05/05',
                //         expiry_date: '2019/05/05'
                //     },{
                //         material_className: '通訊群組',
                //         graph_No: '無',
                //         description: 'P22017062902',
                //         stock_unit_name: '個',
                //         effective_date: '2019/05/05',
                //         expiry_date: '2019/05/05'
                //     }],
                //     QT02_deal_details:[{
                //         deal_date: '2019/05/05',
                //         deal_type: '6.廠內',
                //         deal_no: 'KS11-20190505001-2',
                //         deal_partner_name: '製造',
                //         deal_qty: '9999',
                //         unit_name: '個',
                //         warehouse_name: '成品庫',
                //         warehouse_position: '成品庫-1',
                //         wo_no: 'P251-158511501'
                //     },{
                //         deal_date: '2019/05/05',
                //         deal_type: '6.廠內',
                //         deal_no: 'KS11-20190505001-3',
                //         deal_partner_name: '製造',
                //         deal_qty: '999',
                //         unit_name: '個',
                //         warehouse_name: '成品庫',
                //         warehouse_position: '成品庫-2',
                //         wo_no: 'P251-158511502'
                //     }],
                //     QT02_stock_details:[{
                //         warehouse_name: '成品倉',
                //         warehouse_position: '成品倉-1',
                //         unit_name: '個',
                //         stock_qty: '9997'
                //     },{
                //         warehouse_name: '成品倉',
                //         warehouse_position: '成品倉-15',
                //         unit_name: '個',
                //         stock_qty: '997'
                //     }],
                //     QT02_employee_details:[{
                //         employee_no: 'DR001',
                //         employee_name: '王曉明',
                //         work_time: '2542',
                //         work_qty: '15'
                //     },{
                //         employee_no: 'DR002',
                //         employee_name: '王真',
                //         work_time: '1542',
                //         work_qty: '35'
                //     }],
                //     QT02_material_details:[{
                //         materialOpt_time: '2019/05/05 14:25:01',
                //         materialOpt_type: '領料單(工單)',
                //         machine_no: 'TA01',
                //         machine_name: '落料A',
                //         material_no: 'JC2001-1',
                //         material_name: 'ABS塑膠粒8502',
                //         material_plot_no: 'P200111017062901',
                //         use_qty: '10'
                //     },{
                //         materialOpt_time: '2019/05/05 14:25:01',
                //         materialOpt_type: '領料單(工單)',
                //         machine_no: 'TA02',
                //         machine_name: '落料A2',
                //         material_no: 'JC2001-2',
                //         material_name: 'ABS塑膠粒8502',
                //         material_plot_no: 'P200111017062902',
                //         use_qty: '10'
                //     }],
                //     QT02_productMsg_machine_details:[{
                //         machine_no: 'TA01',
                //         machine_name: '落料A',
                //         work_time: '4542',
                //         work_qty: '15'
                //     },{
                //         machine_no: 'TA02',
                //         machine_name: '落料A2',
                //         work_time: '7542',
                //         work_qty: '35'
                //     }],
                //     QT02_inspectMsg_machine_details:[{
                //         machine_no: 'PESC001',
                //         machine_name: 'PES切片設備01',
                //         check_time: '2019/05/05 14:41:12',
                //         check_list: '切刀點檢項目',
                //         check_no: 'PES_20190505144112',
                //         employee_no: '王真',
                //         qualify_qty: '4',
                //         defect_qty: '0'
                //     },{
                //         machine_no: 'P001',
                //         machine_name: 'P沖壓設備01',
                //         check_time: '2019/05/05 14:41:12',
                //         check_list: '切刀點檢項目',
                //         check_no: 'PES_20190505144112',
                //         employee_no: '王真',
                //         qualify_qty: '12',
                //         defect_qty: '2'
                //     }],
                //     QT02_plotNo_details:[{
                //         plot_no: 'P251-158511501-001',
                //         inspect_time: '2019/05/05 14:25:01',
                //         inspect_type: '首檢',
                //         inspect_result: '合格',
                //         inspect_no: '20190505142501',
                //         employee_no: '王曉明',
                //         op_no: '0010',
                //         op_name: '切片',
                //         sample_qty: '1',
                //         defect_qty: '0'
                //     },{
                //         plot_no: 'P251-158511501-002',
                //         inspect_time: '2019/05/05 14:25:01',
                //         inspect_type: '巡檢',
                //         inspect_result: '不合格',
                //         inspect_no: '20190505142501',
                //         employee_no: '王曉明',
                //         op_no: '0010',
                //         op_name: '切片',
                //         sample_qty: '5',
                //         defect_qty: '3'
                //     }]
                // }
                // if(callback)
                //     callback(data);

                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_LEAN.product_tracing_data_info_get',
                    content : content,
                    success: function (data) {
                        if (data.QT02_collectInfo_details && data.QT02_collectInfo_details.length > 0) {
                            data.QT02_collectInfo_details.forEach(c => {
                                c.QTY = c.QTY + '';
                            });
                        }
                        if (data.QT02_spcInfo_details && data.QT02_spcInfo_details.length > 0) {
                            data.QT02_spcInfo_details.forEach(s => {
                                s.QTY = s.QTY + '';
                                s.GOODQTY = s.GOODQTY + '';
                                s.DEFECTQTY = s.DEFECTQTY + '';
                                s.SAMPLEQTY = s.SAMPLEQTY + '';
                                s.SCRAPQTY = s.SCRAPQTY + '';
                                s.TESTVALUE = s.TESTVALUE + '';
                                if (s.ITEMTYPE == 0) {
                                    s.ITEMTYPE = $filter('translate')('dashboard-QT01.spcInfo.translateName.column1')
                                } else {
                                    s.ITEMTYPE = $filter('translate')('dashboard-QT01.spcInfo.translateName.column2')
                                }
                                if (s.QCformTYPE == '1') {
                                    s.QCformTYPE = $filter('translate')('dashboard-QT01.spcInfo.translateName.column3')
                                } else {
                                    s.QCformTYPE = $filter('translate')('dashboard-QT01.spcInfo.translateName.column4')
                                }
                                if (s.INSPTYPE == '0') {
                                    s.INSPTYPE = $filter('translate')('dashboard-QT01.spcInfo.translateName.column5')
                                } else if (s.INSPTYPE == '1') {
                                    s.INSPTYPE = $filter('translate')('dashboard-QT01.spcInfo.translateName.column6')
                                } else if (s.INSPTYPE == '2') {
                                    s.INSPTYPE = $filter('translate')('dashboard-QT01.spcInfo.translateName.column7')
                                } else if (s.INSPTYPE == '3') {
                                    s.INSPTYPE = $filter('translate')('dashboard-QT01.spcInfo.translateName.column8')
                                } else {
                                    s.INSPTYPE = $filter('translate')('dashboard-QT01.spcInfo.translateName.column9')
                                }
                                if (s.QCITEMTYPE == 0) {
                                    s.QCITEMTYPE = $filter('translate')('dashboard-QT01.spcInfo.translateName.column10')
                                } else if (s.QCITEMTYPE == 1) {
                                    s.QCITEMTYPE = $filter('translate')('dashboard-QT01.spcInfo.translateName.column11')
                                } else {
                                    s.QCITEMTYPE = $filter('translate')('dashboard-QT01.spcInfo.translateName.column12')
                                }
                                if (s.RESULT == 0) {
                                    s.RESULT = $filter('translate')('dashboard-QT01.spcInfo.translateName.column13')
                                } else if (s.RESULT == 1) {
                                    s.RESULT = $filter('translate')('dashboard-QT01.spcInfo.translateName.column14')
                                } else {
                                    s.RESULT = $filter('translate')('dashboard-QT01.spcInfo.translateName.column12')
                                }
                            });
                        }
                        if (data.QT02_tradeInfo_details && data.QT02_tradeInfo_details.length > 0) {
                            data.QT02_tradeInfo_details.forEach(t => {
                                t.QTY = t.QTY + '';
                            });
                        }
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 開窗顯示模版資料

            // 檢驗信息 - 生產批
            $scope.showGrid_plotNoData = function(event, moduleData){
                event.stopPropagation();
                $mdDialog.dialog(
                    'module/moduleQT/program/dashboard-QT02/showGridData/QT02_plotNo.tmp.html',
                    function (dialog){
                        return{
                            moduleData: moduleData,
                            List_PageConfig: {
                                page: 0,
                                page_count: 25
                            },
                            plotNo_filter: {},
                            back: function(){
                                dialog.hide();
                            },
                            export: function(){
                                $scope.export();
                            },
                            getPages: function(list, PageConfig, filterItem){
                                if (list) {
                                    var temp_list = list.filter(function (item, index, array) {
                                        var isOK = true,
                                            keys = Object.keys(filterItem);
                                        for (var i = 0; i < keys.length; i++) {
                                            var key = keys[i];
                                            if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
                                                if (item[key].indexOf(filterItem[key]) == -1) {
                                                    isOK = false;
                                                    break;
                                                }
                                            }
                                        }
                                        return isOK;
                                    });
                                    if (PageConfig) {
                                        if (temp_list.length != 0) {
                                            PageConfig.total_page = Math.ceil(temp_list.length / PageConfig.page_count);

                                            if (PageConfig.page >= PageConfig.total_page) {
                                                PageConfig.page = 0;
                                            }
                                            var index = PageConfig.page * PageConfig.page_count;
                                            return temp_list.slice(index, index + PageConfig.page_count);
                                        } else {
                                            PageConfig.total_page = 1;
                                            PageConfig.page = 0;
                                        }
                                    } else {
                                        return temp_list;
                                    }
                                } else {
                                    return list;
                                }
                            }
                        }
                    }
                );
            };

            // 檢驗信息 - 設備
            $scope.showGrid_inspectMsg_machineData = function(event, moduleData){
                event.stopPropagation();
                $mdDialog.dialog(
                    'module/moduleQT/program/dashboard-QT02/showGridData/QT02_inspectMsg_machine.tmp.html',
                    function (dialog){
                        return{
                            moduleData: moduleData,
                            List_PageConfig: {
                                page: 0,
                                page_count: 25
                            },
                            inspectMsg_machine_filter: {},
                            back: function(){
                                dialog.hide();
                            },
                            export: function(){
                                $scope.export();
                            },
                            getPages: function(list, PageConfig, filterItem){
                                if (list) {
                                    var temp_list = list.filter(function (item, index, array) {
                                        var isOK = true,
                                            keys = Object.keys(filterItem);
                                        for (var i = 0; i < keys.length; i++) {
                                            var key = keys[i];
                                            if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
                                                if (item[key].indexOf(filterItem[key]) == -1) {
                                                    isOK = false;
                                                    break;
                                                }
                                            }
                                        }
                                        return isOK;
                                    });
                                    if (PageConfig) {
                                        if (temp_list.length != 0) {
                                            PageConfig.total_page = Math.ceil(temp_list.length / PageConfig.page_count);

                                            if (PageConfig.page >= PageConfig.total_page) {
                                                PageConfig.page = 0;
                                            }
                                            var index = PageConfig.page * PageConfig.page_count;
                                            return temp_list.slice(index, index + PageConfig.page_count);
                                        } else {
                                            PageConfig.total_page = 1;
                                            PageConfig.page = 0;
                                        }
                                    } else {
                                        return temp_list;
                                    }
                                } else {
                                    return list;
                                }
                            }
                        }
                    }
                );
            };

            // 生產信息 - 設備
            $scope.showGrid_productMsg_machineData = function(event, moduleData){
                event.stopPropagation();
                $mdDialog.dialog(
                    'module/moduleQT/program/dashboard-QT02/showGridData/QT02_productMsg_machine.tmp.html',
                    function (dialog){
                        return{
                            moduleData: moduleData,
                            List_PageConfig: {
                                page: 0,
                                page_count: 25
                            },
                            productMsg_machine_filter: {},
                            back: function(){
                                dialog.hide();
                            },
                            export: function(){
                                $scope.export();
                            },
                            getPages: function(list, PageConfig, filterItem){
                                if (list) {
                                    var temp_list = list.filter(function (item, index, array) {
                                        var isOK = true,
                                            keys = Object.keys(filterItem);
                                        for (var i = 0; i < keys.length; i++) {
                                            var key = keys[i];
                                            if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
                                                if (item[key].indexOf(filterItem[key]) == -1) {
                                                    isOK = false;
                                                    break;
                                                }
                                            }
                                        }
                                        return isOK;
                                    });
                                    if (PageConfig) {
                                        if (temp_list.length != 0) {
                                            PageConfig.total_page = Math.ceil(temp_list.length / PageConfig.page_count);

                                            if (PageConfig.page >= PageConfig.total_page) {
                                                PageConfig.page = 0;
                                            }
                                            var index = PageConfig.page * PageConfig.page_count;
                                            return temp_list.slice(index, index + PageConfig.page_count);
                                        } else {
                                            PageConfig.total_page = 1;
                                            PageConfig.page = 0;
                                        }
                                    } else {
                                        return temp_list;
                                    }
                                } else {
                                    return list;
                                }
                            }
                        }
                    }
                );
            };

            // 生產信息 - 人員
            $scope.showGrid_employeeData = function(event, moduleData){
                event.stopPropagation();
                $mdDialog.dialog(
                    'module/moduleQT/program/dashboard-QT02/showGridData/QT02_employee.tmp.html',
                    function (dialog){
                        return{
                            moduleData: moduleData,
                            List_PageConfig: {
                                page: 0,
                                page_count: 25
                            },
                            employee_filter: {},
                            back: function(){
                                dialog.hide();
                            },
                            export: function(){
                                $scope.export();
                            },
                            getPages: function(list, PageConfig, filterItem){
                                if (list) {
                                    var temp_list = list.filter(function (item, index, array) {
                                        var isOK = true,
                                            keys = Object.keys(filterItem);
                                        for (var i = 0; i < keys.length; i++) {
                                            var key = keys[i];
                                            if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
                                                if (item[key].indexOf(filterItem[key]) == -1) {
                                                    isOK = false;
                                                    break;
                                                }
                                            }
                                        }
                                        return isOK;
                                    });
                                    if (PageConfig) {
                                        if (temp_list.length != 0) {
                                            PageConfig.total_page = Math.ceil(temp_list.length / PageConfig.page_count);

                                            if (PageConfig.page >= PageConfig.total_page) {
                                                PageConfig.page = 0;
                                            }
                                            var index = PageConfig.page * PageConfig.page_count;
                                            return temp_list.slice(index, index + PageConfig.page_count);
                                        } else {
                                            PageConfig.total_page = 1;
                                            PageConfig.page = 0;
                                        }
                                    } else {
                                        return temp_list;
                                    }
                                } else {
                                    return list;
                                }
                            }
                        }
                    }
                );
            };

            // 生產信息 - 物料
            $scope.showGrid_materialData = function(event, moduleData){
                event.stopPropagation();
                $mdDialog.dialog(
                    'module/moduleQT/program/dashboard-QT02/showGridData/QT02_material.tmp.html',
                    function (dialog){
                        return{
                            moduleData: moduleData,
                            List_PageConfig: {
                                page: 0,
                                page_count: 25
                            },
                            material_filter: {},
                            back: function(){
                                dialog.hide();
                            },
                            export: function(){
                                $scope.export();
                            },
                            getPages: function(list, PageConfig, filterItem){
                                if (list) {
                                    var temp_list = list.filter(function (item, index, array) {
                                        var isOK = true,
                                            keys = Object.keys(filterItem);
                                        for (var i = 0; i < keys.length; i++) {
                                            var key = keys[i];
                                            if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
                                                if (item[key].indexOf(filterItem[key]) == -1) {
                                                    isOK = false;
                                                    break;
                                                }
                                            }
                                        }
                                        return isOK;
                                    });
                                    if (PageConfig) {
                                        if (temp_list.length != 0) {
                                            PageConfig.total_page = Math.ceil(temp_list.length / PageConfig.page_count);

                                            if (PageConfig.page >= PageConfig.total_page) {
                                                PageConfig.page = 0;
                                            }
                                            var index = PageConfig.page * PageConfig.page_count;
                                            return temp_list.slice(index, index + PageConfig.page_count);
                                        } else {
                                            PageConfig.total_page = 1;
                                            PageConfig.page = 0;
                                        }
                                    } else {
                                        return temp_list;
                                    }
                                } else {
                                    return list;
                                }
                            }
                        }
                    }
                );
            };

            // 生产资讯 - 交易信息
            $scope.showGrid_tradeData = function (event, moduleData) {
                event.stopPropagation();
                $mdDialog.dialog(
                    'module/moduleQT/program/dashboard-QT02/showGridData/QT02_tradeInfo.tmp.html',
                    function (dialog) {
                        return {
                            moduleData: moduleData,
                            List_PageConfig: {
                                page: 0,
                                page_count: 25
                            },
                            trade_filter: {},
                            back: function () {
                                dialog.hide();
                            },
                            export: function () {
                                $scope.export();
                            },
                            getPages: function (list, PageConfig, filterItem) {
                                if (list) {
                                    var temp_list = list.filter(function (item, index, array) {
                                        var isOK = true,
                                            keys = Object.keys(filterItem);
                                        for (var i = 0; i < keys.length; i++) {
                                            var key = keys[i];
                                            if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
                                                if (item[key].indexOf(filterItem[key]) == -1) {
                                                    isOK = false;
                                                    break;
                                                }
                                            }
                                        }
                                        return isOK;
                                    });
                                    if (PageConfig) {
                                        if (temp_list.length != 0) {
                                            PageConfig.total_page = Math.ceil(temp_list.length / PageConfig.page_count);

                                            if (PageConfig.page >= PageConfig.total_page) {
                                                PageConfig.page = 0;
                                            }
                                            var index = PageConfig.page * PageConfig.page_count;
                                            return temp_list.slice(index, index + PageConfig.page_count);
                                        } else {
                                            PageConfig.total_page = 1;
                                            PageConfig.page = 0;
                                        }
                                    } else {
                                        return temp_list;
                                    }
                                } else {
                                    return list;
                                }
                            }
                        }
                    }
                );
            };

            // 生产资讯 - 收集信息
            $scope.showGrid_collectData = function (event, moduleData) {
                event.stopPropagation();
                $mdDialog.dialog(
                    'module/moduleQT/program/dashboard-QT02/showGridData/QT02_collectInfo.tmp.html',
                    function (dialog) {
                        return {
                            moduleData: moduleData,
                            List_PageConfig: {
                                page: 0,
                                page_count: 25
                            },
                            collect_filter: {},
                            back: function () {
                                dialog.hide();
                            },
                            export: function () {
                                $scope.export();
                            },
                            getPages: function (list, PageConfig, filterItem) {
                                if (list) {
                                    var temp_list = list.filter(function (item, index, array) {
                                        var isOK = true,
                                            keys = Object.keys(filterItem);
                                        for (var i = 0; i < keys.length; i++) {
                                            var key = keys[i];
                                            if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
                                                if (item[key].indexOf(filterItem[key]) == -1) {
                                                    isOK = false;
                                                    break;
                                                }
                                            }
                                        }
                                        return isOK;
                                    });
                                    if (PageConfig) {
                                        if (temp_list.length != 0) {
                                            PageConfig.total_page = Math.ceil(temp_list.length / PageConfig.page_count);

                                            if (PageConfig.page >= PageConfig.total_page) {
                                                PageConfig.page = 0;
                                            }
                                            var index = PageConfig.page * PageConfig.page_count;
                                            return temp_list.slice(index, index + PageConfig.page_count);
                                        } else {
                                            PageConfig.total_page = 1;
                                            PageConfig.page = 0;
                                        }
                                    } else {
                                        return temp_list;
                                    }
                                } else {
                                    return list;
                                }
                            }
                        }
                    }
                );
            };

            // 检验项目 - SPC信息
            $scope.showGrid_SPCData = function (event, moduleData) {
                event.stopPropagation();
                $mdDialog.dialog(
                    'module/moduleQT/program/dashboard-QT02/showGridData/QT02_spcInfo.tmp.html',
                    function (dialog) {
                        return {
                            moduleData: moduleData,
                            List_PageConfig: {
                                page: 0,
                                page_count: 25
                            },
                            spc_filter: {},
                            back: function () {
                                dialog.hide();
                            },
                            export: function () {
                                $scope.export();
                            },
                            getPages: function (list, PageConfig, filterItem) {
                                if (list) {
                                    var temp_list = list.filter(function (item, index, array) {
                                        var isOK = true,
                                            keys = Object.keys(filterItem);
                                        for (var i = 0; i < keys.length; i++) {
                                            var key = keys[i];
                                            if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
                                                if (item[key].indexOf(filterItem[key]) == -1) {
                                                    isOK = false;
                                                    break;
                                                }
                                            }
                                        }
                                        return isOK;
                                    });
                                    if (PageConfig) {
                                        if (temp_list.length != 0) {
                                            PageConfig.total_page = Math.ceil(temp_list.length / PageConfig.page_count);

                                            if (PageConfig.page >= PageConfig.total_page) {
                                                PageConfig.page = 0;
                                            }
                                            var index = PageConfig.page * PageConfig.page_count;
                                            return temp_list.slice(index, index + PageConfig.page_count);
                                        } else {
                                            PageConfig.total_page = 1;
                                            PageConfig.page = 0;
                                        }
                                    } else {
                                        return temp_list;
                                    }
                                } else {
                                    return list;
                                }
                            }
                        }
                    }
                );
            };

            function GenerateMindMap(){
                $mdMindMap.load({
                    data:$scope.QT02_dashboard.production_structure[0],
                    node:{
                        label:function(node){
                            if(node.inspect_no){
                                return node.inspect_type + ' / '+ node.inspect_result + ' ('+ (+node.defect_qty) + '/' +  (+node.sample_qty) +')';
                            } else if (node.check_list){
                                return node.machine_no + ' ('+ node.defect_qty + '/' +  node.qualify_qty +')';
                            } else if(node.employee_no){
                                return node.employee_no + ' / ' + node.employee_name; // 人員
                            } else if(node.machine_no){
                                if(node.material_no){ 
                                    return node.material_no + ' / ' + node.material_name; // 物料
                                } else {
                                    return node.machine_no + ' / ' + node.machine_name; // 設備
                                }
                            } else {
                                return node.name||node.op_no||node.lot_no||node.wo_no;
                            }
                        },
                        children:'data',
                        click:function(d, index, callback){
                            //人員
                            if(d.data.type == 'employeeInfo'){
                                dashboardDetails({
                                    base_data: {},
                                    subset_query_conditon:[
                                        {mode_type: 'employee_details', lot_code: d.parent.parent.data.lot_no, op_no: d.parent.data.op_no}
                                  ]
                                }, function(data){
                                    callback(data.QT02_employee_details);
                                });
                            //設備
                            } else if(d.data.type == 'machineInfo'){
                                dashboardDetails({
                                    base_data: {},
                                    subset_query_conditon:[
                                        {mode_type: 'productmsg_machine_details', lot_code: d.parent.parent.data.lot_no, op_no: d.parent.data.op_no}
                                  ]
                                }, function(data){
                                    callback(data.QT02_productMsg_machine_details);
                                });
                            //物料
                            } else if(d.data.type == 'materialInfo'){
                                dashboardDetails({
                                    base_data: {},
                                    subset_query_conditon:[
                                        {mode_type: 'material_details', lot_code: d.parent.parent.data.lot_no, op_no: d.parent.data.op_no}
                                  ]
                                }, function(data){
                                    callback(data.QT02_material_details);
                                });
                            //檢驗 - 生產批
                            } else if(d.data.type == 'plotno_details'){
                                dashboardDetails({
                                    base_data: {},
                                    subset_query_conditon:[
                                        {mode_type: 'plotno_details', lot_code: d.parent.parent.parent.data.lot_no, op_no: d.parent.parent.data.op_no}
                                  ]
                                }, function(data){
                                    callback(data.QT02_plotNo_details);
                                });
                            //檢驗 - 設備
                            } else  if(d.data.type == 'inspectmsg_machine_details'){
                                dashboardDetails({
                                    base_data: {},
                                    subset_query_conditon:[
                                        {mode_type: 'inspectmsg_machine_details', lot_code: d.parent.parent.parent.data.lot_no, op_no: d.parent.parent.data.op_no}
                                  ]
                                }, function(data){
                                    callback(data.QT02_inspectMsg_machine_details);
                                });
                            //檢驗
                            } else  if(d.data.type == 'spcInfo'){
                                callback([
                                    {type:'plotno_details', name:$filter('translate')('QT02.plotno_details')},
                                    {type:'inspectmsg_machine_details', name:$filter('translate')('QT02.inspectmsg_machine_details')}
                                ]);
                            } else if(!d.data.employee_no && !d.data.machine_no && !d.data.check_list && !d.data.inspect_no && d.data.op_no){
                                callback([
                                    {type:'employeeInfo', name:$filter('translate')('QT02.employeeInfo')},
                                    {type:'machineInfo', name:$filter('translate')('QT02.machineInfo')},
                                    {type:'materialInfo', name:$filter('translate')('QT02.materialInfo')},
                                    {type:'spcInfo', name:$filter('translate')('QT02.spcInfo')}
                                    //,{type:'exceptionInfo', name:$filter('translate')('QT02.exceptionInfo')}
                                ]);
                            }

                        }
                    }
                });
            }
        }
];});
